package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);
        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        // 先执行基本分页
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        //条件 构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //  按类型 模糊 查询
        lqw.like(name != null, Setmeal::getName, name);
        // 排序
        lqw.orderByDesc(Setmeal::getCreateTime);
        //执行分页
        setmealService.page(page1, lqw);
        // 构造怎强型分页
        Page<SetmealDto> page2 = new Page<>();
        BeanUtils.copyProperties(page1, page2, "records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> setmealDtos = records.stream().map((record) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            Long categoryId = record.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if (byId != null) {

                setmealDto.setCategoryName(byId.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        page2.setRecords(setmealDtos);

        return R.success(page2);
    }


    /**
     * 修改套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> updateById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.updateByIdd(id);
        return R.success(setmealDto);
    }

    /**
     * 保存修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> saveupdate(@RequestBody SetmealDto setmealDto){
        setmealService.saveUpdateDish(setmealDto);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String>deleWith( @RequestParam List<Long> ids ){
        setmealService.deleWith(ids);
        return  R.success("“删除成功“");
    }

    @GetMapping("/list")
    public R<List<Setmeal>>save(Setmeal setmeal){

        Long categoryId = setmeal.getCategoryId();
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId,categoryId);
        lambdaQueryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        return R.success(list);

    }
}